package com.benyi.util;

import java.util.Calendar;
import java.util.Date;

public class DateUtil {
	 
	public static final int PERIOD_DAY = 1;    //日
	public static final int PERIOD_WEEK = 2;   //周
	public static final int PERIOD_MONTH = 3;  //月	 
	
	public static String toString(Date t) {
		Calendar c = Calendar.getInstance(); 
		c.setTime(t);  
		int y = c.get(Calendar.YEAR);
		int m = c.get(Calendar.MONTH) + 1;
		int d = c.get(Calendar.DAY_OF_MONTH);
		
		int h=c.get(Calendar.HOUR_OF_DAY);
		int min=c.get(Calendar.MINUTE);
		int sec=c.get(Calendar.SECOND);
		
		StringBuilder sb = new StringBuilder();
		sb.append(String.valueOf(y));
		sb.append("-");
		if(m<10) {
			sb.append("0");
		}
		sb.append(String.valueOf(m));
		sb.append("-");
		if(m<10) {
			sb.append("0");
		}
		sb.append(String.valueOf(d));
		//时分秒
		sb.append(" ");
		if(h<10) {
			sb.append("0");
		}
		sb.append(String.valueOf(h));
		sb.append(":");
		if(min<10) {
			sb.append("0");
		}
		sb.append(String.valueOf(min));
		sb.append(":");
		if(sec<10) {
			sb.append("0");
		}
		sb.append(String.valueOf(sec));
		
		return sb.toString();		
	}
	
	public static int getDateInt(Date t) {
		Calendar c = Calendar.getInstance(); 
		c.setTime(t);  
		return getDateInt(c); 
	}
	
	public static int getDateInt(int days) {
		Calendar c = Calendar.getInstance(); 
		
		int d = c.get(Calendar.DAY_OF_MONTH); 
		c.set(Calendar.DAY_OF_MONTH, d + days );
		
		int y = c.get(Calendar.YEAR);
		int m = c.get(Calendar.MONTH) + 1;
		d = c.get(Calendar.DAY_OF_MONTH);
		
		return getDateInt(c); 
	}
	
	public static boolean isFirstDay(int date) {
		Calendar c = toCalendar(date);
		int day = c.get(Calendar.DAY_OF_MONTH);
		if(day == 1) {
			return true;
		}
		return false;
	}
	
	public static boolean isMonday(int date) {
		Calendar c = toCalendar(date);
		int day = c.get(Calendar.DAY_OF_WEEK);
		if(day == Calendar.MONDAY) {
			return true;
		}
		return false;
	}
	
	public static int getNextDateInt(int date) {
		Calendar c = toCalendar(date); 
		int d = c.get(Calendar.DAY_OF_MONTH); 
		c.set(Calendar.DAY_OF_MONTH, d + 1 ); 
		return getDateInt(c); 
	}
	
	public static int getNextDateInt(int date,int add) {
		Calendar c = toCalendar(date); 
		int d = c.get(Calendar.DAY_OF_MONTH); 
		if(add>0) {
			while(add>0) {
				c.set(Calendar.DAY_OF_MONTH, d + 1 ); 
				d = c.get(Calendar.DAY_OF_MONTH); 
				add -- ;
			} 
		}else {
			if(add<0) {
				while(add < 0) {
					c.set(Calendar.DAY_OF_MONTH, d - 1 ); 
					d = c.get(Calendar.DAY_OF_MONTH); 
					add ++ ;
				} 
			}
		} 
		return getDateInt(c); 
	}
	
	public static int getDateInt(Calendar c) {  
	 
		int y = c.get(Calendar.YEAR);
		int m = c.get(Calendar.MONTH) + 1;
		int d = c.get(Calendar.DAY_OF_MONTH);
		String s=String.valueOf(y);
		if(m<10) {
			s=s+"0";
		}
		s = s+String.valueOf(m);
		if(d<10) {
			s=s+"0";
		}
		s = s+String.valueOf(d);
		return Integer.parseInt(s); 
	}
	
	
	public static String toDateStr(int date) {
		int year = (date/100)/100;
		int month = (date/100)%100;
		int day = date % 100;
		
		String str = String.valueOf(year)+"-";
		if(month<10) {
			str=str+"0";
		}
		str=str+String.valueOf(month)+"-";
		if(day<10) {
			str=str+"0";
		}
		str=str+String.valueOf(day);
		return str;
	}
	
	public static int toDateInt(String date) {
		String []s= date.split("-"); 
		int day = Integer.parseInt(s[0]); 
		day = day*100+Integer.parseInt(s[1]);
		day = day*100+Integer.parseInt(s[2]);
		return day;
	}
	
	public static Calendar toCalendar(int date) {
		int year = (date/100)/100;
		int month = (date/100)%100;
		int day = date % 100;
		Calendar c = Calendar.getInstance(); 
		c.set(Calendar.YEAR, year);
		c.set(Calendar.MONTH,month-1);
		c.set(Calendar.DAY_OF_MONTH, day);
		return c;
	}
	
	//获取 date 所在周的 周一 日期
	public static int toNearWeekMonday(int date) {
		Calendar c = toCalendar(date);
		while(c.get(Calendar.DAY_OF_WEEK) != Calendar.MONDAY) {
			c.add(Calendar.DAY_OF_MONTH, -1);
		}
		return getDateInt(c);
	}
	
	public static int toNearWeekSunday(int date,int maxDay) {
		Calendar c = toCalendar(date);
		if(getDateInt(c)<maxDay) {
			while(c.get(Calendar.DAY_OF_WEEK) != Calendar.SUNDAY) {
				c.add(Calendar.DAY_OF_MONTH, 1);
				if(getDateInt(c)==maxDay) {
					c.add(Calendar.DAY_OF_MONTH, -1);
					break;
				}
			}
		}
		return getDateInt(c);
	}
	
	public static int toNearMonthFirstDay(int date) {
		Calendar c = toCalendar(date);
		while(c.get(Calendar.DAY_OF_MONTH) != 1) {
			c.add(Calendar.DAY_OF_MONTH, -1);
		}
		return getDateInt(c);
	}
	
	public static int toMonthLastDay(int date) {
		Calendar c = toCalendar(date);
		int m = c.get(Calendar.MONTH);
		while(m == c.get(Calendar.MONTH)) {
			c.add(Calendar.DAY_OF_MONTH, 1); 
		}
		c.add(Calendar.DAY_OF_MONTH, -1); 
		return getDateInt(c);
	}
	
	public static int toNearMonthLastDay(int date,int maxDay) {  
		int newDate;
		Calendar c = toCalendar(date);
		if(getDateInt(c)<maxDay) {
			while(c.get(Calendar.DAY_OF_MONTH) != 1) {
				c.add(Calendar.DAY_OF_MONTH, 1);
				newDate = getDateInt(c);
				if(newDate == maxDay) {//等于最大日期,后退1天
					c.add(Calendar.DAY_OF_MONTH, -1);
					break;
				}
			}
			if(c.get(Calendar.DAY_OF_MONTH) == 1) {
				c.add(Calendar.DAY_OF_MONTH, -1); //向后退一天
			}
		}
		return getDateInt(c);
	}
	
	public static int backLastWeek(int day,int add) {
		Calendar c = toCalendar(day);
		while(add>0) {
			c.add(Calendar.DAY_OF_MONTH, -7);
			add--;
		}
		int d = getDateInt(c);
		return d;
	}
	
	public static int backLastMonth(int day,int add) {
		Calendar c = toCalendar(day);
		while(add>0) {
			c.add(Calendar.MONTH, -1);
			add--;
		}
		int d = getDateInt(c);
		return d;
	}
	
	public static int getPeroidFromDay(int type,int add) {
		if(type == PERIOD_DAY) {
			int d = getDateInt(add*(-1));
			return d;
		}
		int toDay  = getDateInt(0); 
		if(type == PERIOD_WEEK) { 
			int monDay = toNearWeekMonday(toDay);
			monDay = backLastWeek(monDay,add);
			return monDay;
		} 
		
		int firstDay = toNearMonthFirstDay(toDay);
		int fromDay = backLastMonth(firstDay,add);
		
		return fromDay;
	}
	
	public static int getPeroidEndDay(int type,int add) {
		if(type == PERIOD_DAY) {
			int d = getDateInt(add*(-1));
			return d;
		}
		int toDay  = getDateInt(0); 
		if(type == PERIOD_WEEK) {
			int monDay = toNearWeekMonday(toDay);
			monDay = backLastWeek(monDay,add);
			int sunDay = getNextDateInt(monDay,6); //加 6 天
			return sunDay;
		}
		
		int firstDay = toNearMonthFirstDay(toDay);
		int fromDay  = backLastMonth(firstDay,add);
		int endDay   = toMonthLastDay(fromDay);
		
		return endDay; 
	}
	
	public static void main(String []args) { 
		System.out.println(getPeroidFromDay(3,0) +"\t"  + getPeroidEndDay(3,0)); 
		System.out.println(getPeroidFromDay(3,1) +"\t"  + getPeroidEndDay(3,1)); 
		System.out.println(getPeroidFromDay(2,10)+"\t"  + getPeroidEndDay(2,10));  
	}
}
